//
//  PKRegistViewController.m
//  PianKe
//
//  Created by lanou on 15/12/24.
//  Copyright © 2015年 KFC. All rights reserved.
//

#import "PKRegistViewController.h"
#import "PKPersonModel.h"
@interface PKRegistViewController ()



@property (strong, nonatomic) UIButton *manButton;
@property (strong, nonatomic) UIButton *womanButton;

@property (strong, nonatomic) PKLTView *username;
@property (strong, nonatomic) PKLTView *emailbox;
@property (strong, nonatomic) PKLTView *password;

@property (strong, nonatomic) UIButton *headButton;

@end

@implementation PKRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UIButton *backButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    backButton.frame = CGRectMake(10*kWidthBase, 30*kHeightBase, 30*kWidthBase, 30*kHeightBase);
    [backButton setBackgroundImage:[UIImage imageNamed:@"fanhui"] forState:(UIControlStateNormal)];
    [self.view addSubview:backButton];
    [backButton addTarget:self action:@selector(back:) forControlEvents:(UIControlEventTouchUpInside)];
    
    

    // 头像
    _headButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    _headButton.frame = CGRectMake((kScreenWidth - 64*kWidthBase) / 2, 80*kHeightBase, 64*kWidthBase, 64*kWidthBase);
    _headButton.layer.cornerRadius = 32*kWidthBase;
    [_headButton setBackgroundImage:[UIImage imageNamed:@"iconfont-jiahaotouxiang"] forState:(UIControlStateNormal)];
    [self.view addSubview:_headButton];
    
    
    
    
    _username = [[PKLTView alloc]initWithFrame:CGRectMake(80*kWidthBase, 250*kHeightBase, kScreenWidth - 160*kWidthBase, 40*kHeightBase) label:@"昵称:" textField:@"请输入昵称名"];
    [self.view addSubview:_username];
    
    _emailbox = [[PKLTView alloc]initWithFrame:CGRectMake(_username.frame.origin.x , _username.frame.origin.y + _username.frame.size.height + 20*kHeightBase, kScreenWidth - 160*kWidthBase, 40*kHeightBase) label:@"邮箱:" textField:@"请输入邮箱"];
    [self.view addSubview:_emailbox];
    
    _password = [[PKLTView alloc]initWithFrame:CGRectMake(_emailbox.frame.origin.x , _emailbox.frame.origin.y + _emailbox.frame.size.height + 20*kHeightBase, kScreenWidth - 160*kWidthBase, 40*kHeightBase) label:@"密码:" textField:@"请输入密码"];
    [self.view addSubview:_password];
    
    
    UIButton *finishButton = [UIButton buttonWithType:UIButtonTypeCustom];
    finishButton.frame = CGRectMake(_emailbox.frame.origin.x, _password.frame.origin.y + _password.frame.size.height + 40*kHeightBase, _emailbox.frame.size.width, _emailbox.frame.size.height);
    
    finishButton.backgroundColor = [UIColor tanguolu];
    [finishButton setTitle:@"完成" forState:UIControlStateNormal];
    [finishButton addTarget:self action:@selector(finishDone:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:finishButton];
    
    
    // 性别按钮
    
    self.manButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.manButton.frame = CGRectMake(_username.left + 20*kWidthBase, _username.top - 50*kHeightBase, 55*kWidthBase, 20*kHeightBase);
    self.manButton.backgroundColor = [UIColor grayColor];
    [self.manButton setTitle:@"♂ 男" forState:(UIControlStateNormal)];
    self.manButton.titleLabel.font = [UIFont systemFontOfSize:12];
    self.manButton.layer.cornerRadius = 9;
    [self.view addSubview:self.manButton];
    [self.manButton addTarget:self action:@selector(exchageSex1:) forControlEvents:(UIControlEventTouchUpInside)];
    
    
    
    self.womanButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.womanButton.frame = CGRectMake(self.manButton.right + 60*kWidthBase, self.manButton.top, 55*kWidthBase, 20*kHeightBase);
    self.womanButton.backgroundColor = [UIColor grayColor];
    [self.womanButton setTitle:@"♀ 女" forState:(UIControlStateNormal)];
    self.womanButton.titleLabel.font = [UIFont systemFontOfSize:12];
    self.womanButton.layer.cornerRadius = 9;
    [self.view addSubview:self.womanButton];
    [self.womanButton addTarget:self action:@selector(exchageSex2:) forControlEvents:(UIControlEventTouchUpInside)];
   

    
    
}


- (void)exchageSex1:(UIButton *)button
{
    button.backgroundColor = [UIColor tiankonglan];
    self.womanButton.backgroundColor = [UIColor grayColor];
}

- (void)exchageSex2:(UIButton *)button
{
    button.backgroundColor = [UIColor tiankonglan];
    self.manButton.backgroundColor = [UIColor grayColor];
}



#pragma mark - 返回上一个界面
- (void)back:(UIButton *)button{
    
    [self.navigationController popViewControllerAnimated:YES];
    
    
}




- (void)finishDone:(UIButton *)sender{
    
    // 实现注册的功能
    [self registerPerson];
 
    
}

// 请求数据,获取用户信息
- (void)registerPerson
{
    
    NSMutableDictionary *parDic = [NSMutableDictionary dictionary];
    parDic[@"auth"] = @"";
    parDic[@"client"] = @"1";
    parDic[@"deviceid"] = @"63A94D37-33F9-40FF-9EBB-481182338873";
    parDic[@"email"] = _emailbox.textField.text;
    parDic[@"uname"] = _username.textField.text;
    parDic[@"passwd"] = _password.textField.text;
    parDic[@"version"] = @"3.0.6";
    

    [PKRequestManager requestWithUrlString:@"http://api2.pianke.me/user/reg" parDic:parDic Method:POST finish:^(NSData *data) {
        
        
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
        NSDictionary *dataDic = dic[@"data"];
        
        if ([dataDic count] == 1) {
        
            if ([dataDic[@"msg"]isEqualToString:@"该邮箱已经注册过了"]) {
                
                
                DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"Sorry" contentText:@"该邮箱已经注册过了" leftButtonTitle:nil rightButtonTitle:@"Fine"];
                [alert show];
                
                
            }else if([dataDic[@"msg"]isEqualToString:@"昵称已经被人占用了"]){
              
                DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"Sorry" contentText:@"昵称已经被人占用了" leftButtonTitle:nil rightButtonTitle:@"Fine"];
                [alert show];
                
                
            }
            
        }else {
            
            DXAlertView *alert = [[DXAlertView alloc] initWithTitle:@"Congratulations" contentText:@"注册成功!" leftButtonTitle:nil rightButtonTitle:@"Fine"];
            [alert show];
            
            alert.rightBlock = ^() {
                
                PKPersonModel *model = [[PKPersonModel alloc] init];
                [model setValuesForKeysWithDictionary:dataDic];
                // 回到登陆界面
                [self.navigationController popViewControllerAnimated:YES];

            
            
            };
        }

        
        
        
    } error:^(NSError *error) {
        
        NSLog(@"%@",error);
    }];
    
    
    
    
    
    
    
}



-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_username.textField resignFirstResponder];
    [_password.textField resignFirstResponder];
    [_emailbox.textField resignFirstResponder];
    
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
